package com.xiao.mp3;

import java.io.File;

import org.farng.mp3.MP3File;
import org.farng.mp3.TagConstant;
import org.farng.mp3.TagOptionSingleton;
import org.farng.mp3.id3.AbstractID3v2;
import org.farng.mp3.id3.ID3v1;

import com.xiao.mp3.util.LoadProperties;

public class ReadMp3File {

	private static String FILE_PATH = LoadProperties.getProperties().getProperty( "filePath");
	
//	public void readFile(){
//		
//		File sourceFile = new File(sourceFile);
//	    MP3File mp3file = new MP3File(sourceFile);
//	    
//		String fileName = "";
//		

//	}
	
	public void modifyMp3(String filePath){
		try {
			File sourceFile = new File(filePath);
			MP3File mp3file = new MP3File(sourceFile);
			ID3v1 v1 = mp3file.getID3v1Tag();
//			ID3v2_3 v2 = (ID3v2_3) mp3file.getID3v2Tag();
//			ID3v1_1 tagID3v1_1 = new ID3v1_1(mp3file);
//		    ID3v1 tagID3v1 = new ID3v1(mp3file);
//		    ID3v2_4 tagID3v2_4 = new ID3v2_4(mp3file);
//		    ID3v2_3 tagID3v2_3 = new ID3v2_3(mp3file);
//		    ID3v2_2 tagID3v2_2 = new ID3v2_2(mp3file);
//		    Lyrics3v2 tagLyrics3v2 = new Lyrics3v2(mp3file);
//		    Lyrics3v1 tagLyrics3v1 = new Lyrics3v1(mp3file);
			AbstractID3v2 id3v2 = mp3file.getID3v2Tag();  
			ID3v1 id3v1 = mp3file.getID3v1Tag();  
			
            if (id3v2 != null) {  
            	TagOptionSingleton.getInstance().setDefaultSaveMode(  
                        TagConstant.MP3_FILE_SAVE_OVERWRITE);
            	
                System.out.println("id3v2");  
                System.out.println(id3v2.getAlbumTitle());//专辑名  
                System.out.println(id3v2.getSongTitle());//歌曲名  
//                System.out.println((new String(id3v2.getSongTitle().getBytes("utf-16"),"ISO-8859-1")));//歌曲名  
                System.out.println(id3v2.getLeadArtist());//歌手  
                id3v2.setSongTitle( "");
                id3v2.setAlbumTitle( "");
                id3v2.setLeadArtist( "");
                id3v2.setSongComment( "");
                mp3file.setID3v2Tag(id3v2);
                TagOptionSingleton.getInstance().setFilenameTagSave(true);
                mp3file.save();
            } else {  
                System.out.println("id3v1");  
                System.out.println(id3v1.getAlbumTitle());  
                System.out.println(id3v1.getSongTitle());  
                System.out.println(id3v1.getLeadArtist());  
  
            }  
  
            System.exit(0);
		} catch (Exception e) {
			System.out.println("modify mp3 error");
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		String fileName = "test.mp3";
		String url = FILE_PATH+fileName;
		try {
			File sourceFile = new File(url);
			MP3File mp3file = new MP3File(sourceFile);
			ID3v1 v1 = mp3file.getID3v1Tag();
//			ID3v2_3 v2 = (ID3v2_3) mp3file.getID3v2Tag();
//			ID3v1_1 tagID3v1_1 = new ID3v1_1(mp3file);
//		    ID3v1 tagID3v1 = new ID3v1(mp3file);
//		    ID3v2_4 tagID3v2_4 = new ID3v2_4(mp3file);
//		    ID3v2_3 tagID3v2_3 = new ID3v2_3(mp3file);
//		    ID3v2_2 tagID3v2_2 = new ID3v2_2(mp3file);
//		    Lyrics3v2 tagLyrics3v2 = new Lyrics3v2(mp3file);
//		    Lyrics3v1 tagLyrics3v1 = new Lyrics3v1(mp3file);
			AbstractID3v2 id3v2 = mp3file.getID3v2Tag();  
			ID3v1 id3v1 = mp3file.getID3v1Tag();  
			
            if (id3v2 != null) {  
            	TagOptionSingleton.getInstance().setDefaultSaveMode(  
                        TagConstant.MP3_FILE_SAVE_OVERWRITE);
            	
                System.out.println("id3v2");  
                System.out.println(id3v2.getAlbumTitle());//专辑名  
                System.out.println(id3v2.getSongTitle());//歌曲名  
                System.out.println((new String(id3v2.getSongTitle().getBytes("utf-16"),"ISO-8859-1")));//歌曲名  
                System.out.println(id3v2.getLeadArtist());//歌手  
                id3v2.setSongTitle( "xxxx");
                id3v2.setAlbumTitle( "au");
                id3v2.setLeadArtist( "li");
                id3v2.setSongComment( "comm");
                mp3file.setID3v2Tag(id3v2);
                
                TagOptionSingleton.getInstance().setFilenameTagSave(true);
                
//                mp3file.save();
            } else {  
                System.out.println("id3v1");  
  
                System.out.println(id3v1.getAlbumTitle());  
                System.out.println(id3v1.getSongTitle());  
                System.out.println(id3v1.getLeadArtist());  
  
            }  
  
//            AbstractLyrics3 lrc3Tag = mp3file.getLyrics3Tag();  
//            if (lrc3Tag != null) {  
//                String lyrics = lrc3Tag.getSongLyric();  
//                System.out.println(lyrics);  
//            }
            System.exit(0);
		} catch (Exception e) {
			System.out.println("error");
			e.printStackTrace();
		}
	}
}
